package command;
import java.util.*;

import Exceptions.TableNotFoundException;
import XMLProcessing.DatabaseProcessing;
import XMLProcessing.TableProcessing;
public class UpdateStatement implements IStatement {
	private String table;
	private TreeMap<String,String> update_list;
	private ConditionNode c;
	public UpdateStatement (String table, TreeMap<String,String> update_list){
		this.table = table;
		this.update_list = update_list;
		c = null;
	}
	
	public UpdateStatement (String table, TreeMap<String,String> update_list, ConditionNode c){
		this.table = table;
		this.update_list = update_list;
		this.c = c;
	}
	
	
	public String toString() {
		String a;
		a = "Update " + table;
		for(String t: update_list.keySet()) {
			a+= t + " = " + update_list.get(t) + "\n";	
		}
		return a;
	}

	@Override
	public ResultNode eval() throws TableNotFoundException{
		String xmlFile = DatabaseProcessing.retrieveTable(this.table);
		ResultNode rN = null;
		
		Integer a = TableProcessing.updateTable(xmlFile, update_list, c);
		
		
		rN = new ResultNode(null,null,a);
		
		return rN;
	}
	
}
